/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-02 19:40:58
 * Copyright (c) 2015- joysuch-版权所有
 */
package com.joysuch.wwyt.bp.service.impl;

import cn.hutool.core.util.StrUtil;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.joysuch.wwyt.api.basedata.service.BpDepartSyncService;
import com.joysuch.wwyt.bp.bean.BpEnterpriseInfoAddBean;
import com.joysuch.wwyt.bp.bean.BpEnterpriseInfoBean;
import com.joysuch.wwyt.bp.entity.BpDepart;
import com.joysuch.wwyt.bp.entity.BpEnterpriseInfo;
import com.joysuch.wwyt.bp.entity.contractor.BpContractorConstructionRecord;
import com.joysuch.wwyt.bp.entity.inspect.BPInspectItem;
import com.joysuch.wwyt.bp.enums.DataStateTypes;
import com.joysuch.wwyt.bp.repository.BpDepartDao;
import com.joysuch.wwyt.bp.repository.BpEnterpriseInfoDao;
import com.joysuch.wwyt.bp.service.BpEnterpriseInfoService;
import com.joysuch.wwyt.common.constant.Constant;
import com.joysuch.wwyt.core.bean.BaseAttachmentBean;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.web.PageableArgumentResolver;
import com.joysuch.wwyt.core.entity.BaseConfig;
import com.joysuch.wwyt.core.entity.BaseImageRepository;
import com.joysuch.wwyt.core.enums.BaseConfigCodes;
import com.joysuch.wwyt.core.repository.BaseImageRespositoryDao;
import com.joysuch.wwyt.core.repository.BaseRegionDao;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.core.service.BaseConfigService;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.util.SnowFlakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class BpEnterpriseInfoServiceImpl implements BpEnterpriseInfoService {

    @Autowired
    private BpEnterpriseInfoDao dao;
    @Autowired
    private BaseImageRespositoryDao imagedao;
    @Autowired
    private BaseRegionDao regionDao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;
    @Value("${wwyt.base.serverUrl}")
    private String serverUrl;
    @Autowired
    private BaseUserDao baseUserDao;
    @Autowired
    private BpDepartDao bpDepartDao;
    @Autowired
    private BaseConfigService baseConfigService;
    @Autowired
    private BpDepartSyncService departSyncService;

    @Override
    public void add(BpEnterpriseInfo entity) throws Exception {
        dao.save(entity);
    }

    @Override
    public void update(BpEnterpriseInfoAddBean e) throws Exception {
        String economicType1 = e.getEconomicType1();
        String economicType1Result = Strings.emptyToNull(economicType1);
        String tradeType1 = e.getTradeType1();
        String tradeType1Result = Strings.emptyToNull(tradeType1);
        String superviseType1 = e.getSuperviseType1();
        String superviseType1Result = Strings.emptyToNull(superviseType1);

        BpEnterpriseInfo b;
        if (e.getId() != null) {
            b = dao.getOne(e.getId());
            b.setBusinessAddress(e.getBusinessAddress());
            b.setBusinessScope(e.getBusinessScope());
            b.setCompanyType(e.getCompanyType());

            b.setEconomicType1(economicType1Result);
            b.setEmail(e.getEmail());
            b.setFax(e.getFax());
            b.setFloorArea(e.getFloorArea());
            if (e.getPicture() != null) {
                b.setPictureId(e.getPicture().getId());
            }
            b.setHasFullTimeSafetyStaff(e.getHasFullTimeSafetyStaff());
            b.setHasHchAccidentIn3years(e.getHasHchAccidentIn3years());
            b.setHasHchTunnel(e.getHasHchTunnel());
            b.setWebsite(e.getWebsite());
            b.setUnifiedSocialCreditCode(e.getUnifiedSocialCreditCode());
            b.setTradeType1(tradeType1Result);
            b.setTelephone(e.getTelephone());
            b.setSurroundingType(e.getSurroundingType());
            b.setSurroundingRemark(e.getSurroundingRemark());
            b.setSurroundingPeopleCount(e.getSurroundingPeopleCount());
            b.setSurroundingName(e.getSurroundingName());
            b.setSurroundingMinDistance(e.getSurroundingMinDistance());
            b.setSurroundingLongitudeLatitude(e.getSurroundingLongitudeLatitude());
            b.setSurroundingDirection(e.getSurroundingDirection());
            b.setSurroundingContactPhone(e.getSurroundingContactPhone());
            b.setSurroundingContactName(e.getSurroundingContactName());
            b.setSurroundingContactMobile(e.getSurroundingContactMobile());
            b.setSurroundingContactEmail(e.getSurroundingContactEmail());
            b.setSurroundingArchitecture(e.getSurroundingArchitecture());
            b.setSurroundingAdjacentBuildingHeight(e.getSurroundingAdjacentBuildingHeight());
            b.setSuperviseType1(superviseType1Result);
            b.setStStaffCount(e.getStStaffCount());
            b.setStaffCountConfig(e.getStaffCountConfig());
            b.setStSpecialWorkStaffCount(e.getStSpecialWorkStaffCount());
            b.setStRegisteredSafetyEngineerCount(e.getStRegisteredSafetyEngineerCount());
            b.setStPartimeTimeSafetyProductionManagerCount(e.getStPartimeTimeSafetyProductionManagerCount());
            b.setStFullTimeSafetyProductionManagerCount(e.getStFullTimeSafetyProductionManagerCount());
            b.setStFullTimeEmergencyManagerCount(e.getStFullTimeEmergencyManagerCount());
            b.setSecondDoorBuildStatus(e.getSecondDoorBuildStatus());
            b.setScale(e.getScale());
            b.setSafetySuperviseLevel(e.getSafetySuperviseLevel());
            b.setSafetySuperviseInstitution(e.getSafetySuperviseInstitution());
            b.setSafetyManageInstitutionStaffCount(e.getSafetyManageInstitutionStaffCount());
            b.setSafetyManageInstitutionName(e.getSafetyManageInstitutionName());
            b.setSafetyManageInstitutionDuty(e.getSafetyManageInstitutionDuty());
            b.setSafetyLevel(e.getSafetyLevel());
            b.setRiskLevel(e.getRiskLevel());
            b.setRemark(e.getRemark());
            b.setRegisteredCapital(e.getRegisteredCapital());
            if (e.getRegisterDate() != null && !"".equals(e.getRegisterDate())) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date = sdf.parse(e.getRegisterDate());
                b.setRegisterDate(date);
            }
            b.setRegisterAddress(e.getRegisterAddress());
            b.setProvince(e.getProvince());

            b.setProductionPlacePropertyRight(e.getProductionPlacePropertyRight());
            b.setPrincipal(e.getPrincipal());
            b.setPostcode(e.getPostcode());
            b.setParentCompanyName(e.getParentCompanyName());
            b.setMhhchLevel(e.getMhhchLevel());
            b.setManagementState(e.getManagementState());
            b.setLongitudeLatitude(e.getLongitudeLatitude());
            String location = e.getLocation();
            b.setLocationProvince(location);
            b.setLocationStreet(e.getLocationStreet());

            if (e.getLayoutPicture() != null) {
                b.setLayoutPictureId(e.getLayoutPicture().getId());
            }
            b.setLegalRepresentative(e.getLegalRepresentative());
            b.setHasSecondDoor(e.getHasSecondDoor());
            b.setHasMhhch(e.getHasMhhch());

            //所在村(社区)
            b.setCommunity(e.getCommunity());
            //所在园区(开发区)
            b.setDevelopmentZone(e.getDevelopmentZone());
            //隶属关系
            b.setOwnership(e.getOwnership());
            //集团公司名称
            b.setGroupCompanyName(e.getGroupCompanyName());
            //是否有专门的安全机构
            b.setSecurityAgency(e.getSecurityAgency());
            //联系qq号
            b.setQq(e.getQq());

            dao.save(b);
        } else {
            b = new BpEnterpriseInfo();
            b.setBusinessAddress(e.getBusinessAddress());
            b.setBusinessScope(e.getBusinessScope());
            b.setCompanyType(e.getCompanyType());
            b.setEconomicType1(economicType1Result);
            b.setEmail(e.getEmail());
            b.setFax(e.getFax());
            b.setFloorArea(e.getFloorArea());
            if (e.getPicture() != null) {
                b.setPictureId(e.getPicture().getId());
            }
            b.setHasFullTimeSafetyStaff(e.getHasFullTimeSafetyStaff());
            b.setHasHchAccidentIn3years(e.getHasHchAccidentIn3years());
            b.setHasHchTunnel(e.getHasHchTunnel());
            b.setWebsite(e.getWebsite());
            b.setUnifiedSocialCreditCode(e.getUnifiedSocialCreditCode());
            b.setTradeType1(tradeType1Result);
            b.setTelephone(e.getTelephone());
            b.setSurroundingType(e.getSurroundingType());
            b.setSurroundingRemark(e.getSurroundingRemark());
            b.setSurroundingPeopleCount(e.getSurroundingPeopleCount());
            b.setSurroundingName(e.getSurroundingName());
            b.setSurroundingMinDistance(e.getSurroundingMinDistance());
            b.setSurroundingLongitudeLatitude(e.getSurroundingLongitudeLatitude());
            b.setSurroundingDirection(e.getSurroundingDirection());
            b.setSurroundingContactPhone(e.getSurroundingContactPhone());
            b.setSurroundingContactName(e.getSurroundingContactName());
            b.setSurroundingContactMobile(e.getSurroundingContactMobile());
            b.setSurroundingContactEmail(e.getSurroundingContactEmail());
            b.setSurroundingArchitecture(e.getSurroundingArchitecture());
            b.setSurroundingAdjacentBuildingHeight(e.getSurroundingAdjacentBuildingHeight());
            b.setSuperviseType1(superviseType1Result);
            b.setStStaffCount(e.getStStaffCount());
            b.setStSpecialWorkStaffCount(e.getStSpecialWorkStaffCount());
            b.setStRegisteredSafetyEngineerCount(e.getStRegisteredSafetyEngineerCount());
            b.setStPartimeTimeSafetyProductionManagerCount(e.getStPartimeTimeSafetyProductionManagerCount());
            b.setStFullTimeSafetyProductionManagerCount(e.getStFullTimeSafetyProductionManagerCount());
            b.setStFullTimeEmergencyManagerCount(e.getStFullTimeEmergencyManagerCount());
            b.setSecondDoorBuildStatus(e.getSecondDoorBuildStatus());
            b.setScale(e.getScale());
            b.setSafetySuperviseLevel(e.getSafetySuperviseLevel());
            b.setSafetySuperviseInstitution(e.getSafetySuperviseInstitution());
            b.setSafetyManageInstitutionStaffCount(e.getSafetyManageInstitutionStaffCount());
            b.setSafetyManageInstitutionName(e.getSafetyManageInstitutionName());
            b.setSafetyManageInstitutionDuty(e.getSafetyManageInstitutionDuty());
            b.setSafetyLevel(e.getSafetyLevel());
            b.setRiskLevel(e.getRiskLevel());
            b.setRemark(e.getRemark());
            b.setRegisteredCapital(e.getRegisteredCapital());
            if (e.getRegisterDate() != null && !"".equals(e.getRegisterDate())) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date = sdf.parse(e.getRegisterDate());
                b.setRegisterDate(date);
            }
            b.setRegisterAddress(e.getRegisterAddress());
            b.setProvince(e.getProvince());

            b.setProductionPlacePropertyRight(e.getProductionPlacePropertyRight());
            b.setPrincipal(e.getPrincipal());
            b.setPostcode(e.getPostcode());
            b.setParentCompanyName(e.getParentCompanyName());
            b.setName(e.getName());
            b.setMhhchLevel(e.getMhhchLevel());
            b.setManagementState(e.getManagementState());
            b.setLongitudeLatitude(e.getLongitudeLatitude());
            String location = e.getLocation();
            b.setLocationProvince(location);
            b.setLocationStreet(e.getLocationStreet());

            if (e.getLayoutPicture() != null) {
                System.out.println("json = " + e.getLayoutPicture());
                b.setLayoutPictureId(e.getLayoutPicture().getId());
            }
            b.setLegalRepresentative(e.getLegalRepresentative());
            b.setHasSecondDoor(e.getHasSecondDoor());
            b.setHasMhhch(e.getHasMhhch());
        }

        b.setSafeOnDutyTel(e.getSafeOnDutyTel());
        b.setSpecialTreatType(e.getSpecialTreatType());
        b.setEconomicTypeBig(e.getEconomicTypeBig());
        b.setEconomicTypeSmall(e.getEconomicTypeSmall());
        b.setIndustryTypeCategory(e.getIndustryTypeCategory());
        b.setIndustryTypeBig(e.getIndustryTypeBig());
        b.setIndustryTypeMedium(e.getIndustryTypeMedium());
        b.setIndustryTypeSmall(e.getIndustryTypeSmall());
        b.setIndustryMonitorBig(e.getIndustryMonitorBig());
        b.setIndustryMonitorSmall(e.getIndustryMonitorSmall());
        b.setLongitude(e.getLongitude());
        b.setLatitude(e.getLatitude());
        b.setStateEnterpriseOrNot(e.getStateEnterpriseOrNot());
        b.setEnterpriseScope(e.getEnterpriseScope());
        b.setMotherCompanyOrNot(e.getMotherCompanyOrNot());
        b.setMainChargerId(e.getMainChargerId());
        b.setSafeChargerId(e.getSafeChargerId());

        b.setLocationProvince(e.getLocationProvince());
        b.setLocationCity(e.getLocationCity());
        b.setLocationDistrict(e.getLocationDistrict());
        b.setLocationStreet(e.getLocationStreet());

        b.setStandardCertStartDate(e.getStandardCertStartDate());
        b.setStandardCertEndDate(e.getStandardCertEndDate());
        b.setEnterpriseCode(e.getEnterpriseCode());
        //所在村(社区)
        b.setCommunity(e.getCommunity());
        //所在园区(开发区)
        b.setDevelopmentZone(e.getDevelopmentZone());
        //隶属关系
        b.setOwnership(e.getOwnership());
        //集团公司名称
        b.setGroupCompanyName(e.getGroupCompanyName());
        //是否有专门的安全机构
        b.setSecurityAgency(e.getSecurityAgency());
        //联系qq号
        b.setQq(e.getQq());
        BpEnterpriseInfo save = dao.save(b);
        syncDepartName(save.getName());
    }

    @Override
    public void updateName(Long tenentId, String orgCode, String name) {
        if (StrUtil.isBlank(name)) {
            return;
        }
        dao.updateNameByTenentIdAndOrgCode(name, tenentId, orgCode);
        // 更新顶级部门名称
        BpDepart depart = bpDepartDao.getFatherDepart(orgCode, tenentId);
        depart.setName(name);
        bpDepartDao.save(depart);
    }

    //同步顶级部门
    @Override
    public void syncDepartName(String name) {
        this.syncDepartName(name, Context.getCurrentTenantId(), Context.getCompanyCode());
    }

    @Override
    public void syncDepartName(String name, Long tenentId, String orgCode) {
        // 同步人员在岗在位版本必须是 4.X
        BaseConfig config = baseConfigService.findConfig(tenentId, orgCode, BaseConfigCodes.LOCATE_VERSION);
        if (config == null || "3.X".equalsIgnoreCase(config.getValue().trim())) {
            return ;
        }
        if (StringUtils.isNotBlank(orgCode) && tenentId != null) {
            BpDepart depart = bpDepartDao.getFatherDepart(orgCode, tenentId);
            depart.setName(name);
            if (depart.getUniqueId() == null) {
                depart.setUniqueId(SnowFlakeUtil.getDepartId());
                depart.setLocateSync(false);
            }
            if (isEmployeeSyncOpen()) {
                boolean b = departSyncService.updateSync(depart, null);
                depart.setLocateSync(b);
                if (!b) {
                    throw new IllegalArgumentException("顶级部门同步失败,请检查名称和同步Id， uniqueId="+ depart.getUniqueId());
                }
            }
            bpDepartDao.save(depart);
        }
    }

    private boolean isEmployeeSyncOpen() {
        BaseConfig config = baseConfigService.findFirstConfig(BaseConfigCodes.LOCATE_SERVER_EMPLOYEE_SYNC_OPEN);
        return config != null && "true".equalsIgnoreCase(config.getValue());
    }

    /**
     * 获取当前部门信息
     *
     * @return
     */
    @Override
    public List<SimpleSelectListBean> getEnterpriseInfo() {
        BpEnterpriseInfo info = dao.findAllByOrgCodeAndTenentId(Context.getCompanyCode(), Context.getCurrentTenantId());
        SimpleSelectListBean bean = new SimpleSelectListBean();
        if (info != null) {
            bean.setValue(info.getId());
            bean.setLabel(info.getName());
        }
        return Lists.newArrayList(bean);
    }

    @Override
    public List<BpEnterpriseInfoBean> findByPage(Pageable page) {
        BpEnterpriseInfo e = dao.findAllByOrgCodeAndTenentId(Context.getCompanyCode(), Context.getCurrentTenantId());
        ArrayList<BpEnterpriseInfoBean> list = new ArrayList<>();
        BpEnterpriseInfoBean b = new BpEnterpriseInfoBean();
        b.setId(e.getId());
        b.setName(e.getName());
        b.setUnifiedSocialCreditCode(e.getUnifiedSocialCreditCode());
        b.setProvinces(e.getProvince());
        b.setLocation(e.getLocationProvince());
        b.setProvince(e.getProvince());
        b.setLocationStreet(e.getLocationStreet());
        b.setRegisterAddress(e.getRegisterAddress());
        b.setBusinessAddress(e.getBusinessAddress());
        b.setRegisteredCapital(e.getRegisteredCapital());
        b.setFloorArea(e.getFloorArea());
        b.setPostcode(e.getPostcode());
        b.setRegisterDate(e.getRegisterDate());
        b.setLegalRepresentative(e.getLegalRepresentative());
        b.setTelephone(e.getTelephone());
        b.setFax(e.getFax());
        b.setEmail(e.getEmail());
        b.setWebsite(e.getWebsite());
        b.setEconomicType1(e.getEconomicType1() == null ? null : String.valueOf(e.getEconomicType1()));
        b.setTradeType1(e.getTradeType1() == null ? null : String.valueOf(e.getTradeType1()));
        b.setSuperviseType1(e.getSuperviseType1() == null ? null : String.valueOf(e.getSuperviseType1()));
        b.setManagementState(e.getManagementState());
        b.setProductionPlacePropertyRight(e.getProductionPlacePropertyRight());
        b.setScale(e.getScale());
        b.setParentCompanyName(e.getParentCompanyName());
        b.setSafetyLevel(e.getSafetyLevel());
        b.setRiskLevel(e.getRiskLevel());
        b.setSafetySuperviseLevel(e.getSafetySuperviseLevel());
        b.setCompanyType(e.getCompanyType());
        b.setHasMhhch(e.getHasMhhch());
        b.setMhhchLevel(e.getMhhchLevel());
        b.setHasSecondDoor(e.getHasSecondDoor());
        b.setSecondDoorBuildStatus(e.getSecondDoorBuildStatus());
        b.setHasHchAccidentIn3years(e.getHasHchAccidentIn3years());
        b.setHasHchTunnel(e.getHasHchTunnel());
        b.setSafetySuperviseInstitution(e.getSafetySuperviseInstitution());
        b.setLongitudeLatitude(e.getLongitudeLatitude());
        b.setBusinessScope(e.getBusinessScope());
        b.setRemark(e.getRemark());
        b.setPrincipal(e.getPrincipal());
        b.setLayoutPictureId(e.getLayoutPictureId());
        b.setSurroundingType(e.getSurroundingType());
        b.setSurroundingName(e.getSurroundingName());
        b.setSurroundingDirection(e.getSurroundingDirection());
        b.setSurroundingMinDistance(e.getSurroundingMinDistance());
        b.setSurroundingArchitecture(e.getSurroundingArchitecture());
        b.setSurroundingAdjacentBuildingHeight(e.getSurroundingAdjacentBuildingHeight());
        b.setSurroundingPeopleCount(e.getSurroundingPeopleCount());
        b.setSurroundingContactPhone(e.getSurroundingContactPhone());
        b.setSurroundingContactMobile(e.getSurroundingContactMobile());
        b.setSurroundingContactEmail(e.getSurroundingContactEmail());
        b.setSurroundingContactName(e.getSurroundingContactName());
        b.setSurroundingLongitudeLatitude(e.getSurroundingLongitudeLatitude());
        b.setSurroundingRemark(e.getSurroundingRemark());
        if (Constant.COMMON_SWITCH_ON.equals(e.getStaffCountConfig())) {
            b.setStStaffCount(baseUserDao.getEffectiveUserCount());
        }else {
            b.setStStaffCount(e.getStStaffCount());
        }
        b.setStaffCountConfig(e.getStaffCountConfig());
        b.setStSpecialWorkStaffCount(e.getStSpecialWorkStaffCount());
        b.setStFullTimeSafetyProductionManagerCount(e.getStFullTimeSafetyProductionManagerCount());
        b.setStPartimeTimeSafetyProductionManagerCount(e.getStPartimeTimeSafetyProductionManagerCount());
        b.setStFullTimeEmergencyManagerCount(e.getStFullTimeEmergencyManagerCount());
        b.setStRegisteredSafetyEngineerCount(e.getStRegisteredSafetyEngineerCount());
        b.setSafetyManageInstitutionName(e.getSafetyManageInstitutionName());
        b.setSafetyManageInstitutionDuty(e.getSafetyManageInstitutionDuty());
        b.setSafetyManageInstitutionStaffCount(e.getSafetyManageInstitutionStaffCount());
        b.setHasFullTimeSafetyStaff(e.getHasFullTimeSafetyStaff());
        if (e.getLayoutPictureId() != null) {
            BaseAttachmentBean baseAttachmentBean = new BaseAttachmentBean();
            baseAttachmentBean.setId(e.getLayoutPictureId());
            baseAttachmentBean.setDownloadUrl(serverUrl + "/image/view?id=" + e.getLayoutPictureId());
            b.setLayoutPicture(baseAttachmentBean);
        }
        if (e.getPictureId() != null) {
            BaseImageRepository one = imagedao.getOne(e.getPictureId());
            BaseAttachmentBean baseAttachmentBean = new BaseAttachmentBean();
            baseAttachmentBean.setId(e.getPictureId());
            baseAttachmentBean.setDownloadUrl(serverUrl + "/image/view?id=" + e.getPictureId());
            b.setPicture(baseAttachmentBean);
        }

        b.setSafeOnDutyTel(e.getSafeOnDutyTel());
        b.setSpecialTreatType(e.getSpecialTreatType());
        b.setEconomicTypeBig(e.getEconomicTypeBig());
        b.setEconomicTypeSmall(e.getEconomicTypeSmall());
        b.setIndustryTypeCategory(e.getIndustryTypeCategory());
        b.setIndustryTypeBig(e.getIndustryTypeBig());
        b.setIndustryTypeMedium(e.getIndustryTypeMedium());
        b.setIndustryTypeSmall(e.getIndustryTypeSmall());
        b.setIndustryMonitorBig(e.getIndustryMonitorBig());
        b.setIndustryMonitorSmall(e.getIndustryMonitorSmall());
        b.setLongitude(e.getLongitude());
        b.setLatitude(e.getLatitude());
        b.setStateEnterpriseOrNot(e.getStateEnterpriseOrNot());
        b.setEnterpriseScope(e.getEnterpriseScope());
        b.setMotherCompanyOrNot(e.getMotherCompanyOrNot());
        Long mainChargerId = e.getMainChargerId();
        b.setMainChargerId(mainChargerId);
        b.setMainChargerName(mainChargerId == null ? null : baseUserDao.getRealNameById(mainChargerId));
        Long safeChargerId = e.getSafeChargerId();
        b.setSafeChargerId(safeChargerId);
        b.setSafeChargerName(safeChargerId == null ? null : baseUserDao.getRealNameById(safeChargerId));

        b.setLocationProvince(e.getLocationProvince());
        b.setLocationCity(e.getLocationCity());
        b.setLocationDistrict(e.getLocationDistrict());
        b.setLocationStreet(e.getLocationStreet());
        b.setStandardCertStartDate(e.getStandardCertStartDate());
        b.setStandardCertEndDate(e.getStandardCertEndDate());
        b.setEnterpriseCode(e.getEnterpriseCode());
        //所在村(社区)
        b.setCommunity(e.getCommunity());
        //所在园区(开发区)
        b.setDevelopmentZone(e.getDevelopmentZone());
        //隶属关系
        b.setOwnership(e.getOwnership());
        //集团公司名称
        b.setGroupCompanyName(e.getGroupCompanyName());
        //是否有专门的安全机构
        b.setSecurityAgency(e.getSecurityAgency());
        //联系qq号
        b.setQq(e.getQq());
        list.add(b);

        return list;
    }

    @Override
    public void delete(Long id) {
        dao.deleteById(id);
    }

    @Override
    public BpEnterpriseInfo findById(Long id) {
        return dao.findById(id).get();
    }

    @Override
    public BpEnterpriseInfo findByOrgCode(String orgCode) {
        return dao.findByOrgCode(orgCode);
    }

    @Override
    public Integer getCurUserCount() {
        return baseUserDao.getEffectiveUserCount();
    }

    @Override
    public Page<BpEnterpriseInfo> findByIdGreaterThan(Long id, int size) {
        Specification<BpEnterpriseInfo> specification = (Root<BpEnterpriseInfo> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            predicates.add(cb.greaterThan(root.get("id"), id));
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        PageRequest pageRequest = PageRequest.of(0, size, Sort.by(Sort.Direction.ASC, "id"));
        return dao.findAll(specification, pageRequest);
    }

}
